var UnProtectUrl = function (){
	var urlGridObj = null;
	var urlValidator = null;
	function resetForm(rowIndex){
		AppUtils.clearForm("#urlEdit");
		var record =  urlGridObj.getRecord(rowIndex);
		$.each(record,function(key,value){
			if(key == "isRegexp"){
				value = value == "1"? $("#"+key).attr("checked",true):$("#"+key).attr("checked",false);
			}
			$("#"+key).val(value);
		});
	}
	function saveUrlInfo(index){
		urlValidator.form();
		if (!urlValidator.valid()) {
			return false;
		}
		var urlInfo = dwr.util.getFormValues('urlEdit');
		urlInfo.id = $("#id").val()== "" ? null :$("#id").val();
		urlInfo.isRegexp = urlInfo.isRegexp?"1":"0";
		SysUnprotectUrlController.saveUnProtectUrlInfo(urlInfo,function(result){
			if(result){
				Message.success("保存成功！");
				urlGridObj.refreshPage();
			}else{
				Message.error("保存失败！");
			}
			Message.close(index);
		});
	}
	return {
		initUrlTable:function(){
			urlValidator=$("#urlEdit").validate({
				rules:{
					url:{required:true,maxlength:100},
					description:{required:true,maxlength:100,CN:true}
				}
			});
			urlGridObj = $.fn.bsgrid.init('urlTable', {
	            url:contextPath+"/sys/unProtectUrl/list.htm",
	            pageSizeSelect: true,
	            stripeRows: true,
	            rowSelectedColor:false,
	            pageSize: 10
				
	        });
		},
		isRegexp:function(record, rowIndex, colIndex, options){
			return record['isRegexp'] == "1"?"是":"否";
			
		},
		Operate:function(record, rowIndex, colIndex, options){
			return '<a href="#" onclick="UnProtectUrl.editUrl(\''+rowIndex+'\');">编辑</a>&nbsp;&nbsp;<a href="#" onclick="UnProtectUrl.removeUrl(\''+urlGridObj.getRecordIndexValue(record, 'id')+'\');">删除</a>';
		},
		newUrl:function(){
			AppUtils.clearForm("#urlEdit");
			DialogUtils.innerDialog({
				area: ['500px', '320px'],
			    title: '添加路径',
			    content: $("#editUrl"),
			    btn: ['保存', '取消'],
			    yes: function(index, layero){
			    	saveUrlInfo(index);
			    },cancel: function(index){
			    }
			});
		},
		editUrl:function(rowIndex){
			resetForm(rowIndex);
			DialogUtils.innerDialog({
				area: ['500px', '320px'],
			    title: '编辑路径',
			    content: $("#editUrl"),
			    btn: ['保存', '取消'],
			    yes: function(index, layero){
			    	saveUrlInfo(index);
			    },cancel: function(index){
			    }
			});
		},
		removeUrl:function(id){
			Message.confirm("确定删除该路径吗?",function(index){
				SysUnprotectUrlController.removeUnProtectUrlById(id,function(result){
					if(result){
						Message.success("删除成功！");
						urlGridObj.refreshPage();
					}else{
						Message.error("删除失败！");
					}
					Message.close(index);
				});
				
			});
		}
	}
}();